chrome.storage.sync.get("status", function (result) {
	var status = result.status;
	if (status === 0) {
		document.getElementById("status").innerText = "暂停";
	}
	if (status === 1) {
		document.getElementById("status").innerText = "已开始";
	}
	if (status === 2) {
		document.getElementById("status").innerText = "已重置";
	}
});

document.getElementById("clear").onclick = function () {
	chrome.storage.sync.set({ procedure: [] }, function () {
		console.log("清楚成功");
	});
	chrome.storage.sync.set({ status: 2 });
	sendMessageToContentScript({ cmd: "reset", value: "ok" }, function (response) {
		document.getElementById("status").innerText = "已重置";
	});
};
// 0(click) -->1(/project/page)
// 1(/project/page) -->2(click)
// 2(click) -->3(/company/tenderAuthInfo)
// 3(/company/tenderAuthInfo) -->4(/project/detail)
// 4(/project/detail) -->5(/order/loadInsuranceChanel)
// 5(/order/loadInsuranceChanel) -->6(click)
// 6(click) -->7(/order/createOrder)
// 7(/order/createOrder) -->8(undefined)

document.getElementById("complete").onclick = function () {
	// chrome.storage.sync.get("procedure", function (result) {
	// 	var procedure = result.procedure;
	// 	var str = "";
	// 	procedure.forEach((el, index) => {
	// 		str += `${index}(${el})`;
	// 		if (procedure[index + 1]) {
	// 			str += ` -->${index + 1}(${procedure[index + 1]})\n`;
	// 		}
	// 	});
	// 	copy(str);
	// });


	// chrome.storage.sync.set({ status: 2 });
	// sendMessageToContentScript({ cmd: "complete", value: "ok" }, function (response) {
	// 	document.getElementById("status").innerText = "完成";
	// });
};
// complete

/* 

0(click) -->1(/project/page)
1(/project/page) -->2(click)
2(click) -->3(/company/tenderAuthInfo)
3(/company/tenderAuthInfo) -->4(/project/detail)
4(/project/detail) -->5(/order/loadInsuranceChanel)
5(/order/loadInsuranceChanel) -->6(click)
6(click) -->7(/order/createOrder)
7(/order/createOrder)

	1[AAAA] -->2(round edge)
			2 --> C{deasdfasdf}
			C -->|ONE| D[DDDDD]
			C -->|TWO| E[ASDFASDF]
*/
document.getElementById("start").onclick = function () {
	chrome.storage.sync.set({ status: 1 });
	sendMessageToContentScript({ cmd: "start", value: "ok" }, function (response) {
		document.getElementById("status").innerText = "已开始";
	});
};

document.getElementById("suspend").onclick = function () {

	sendMessageToContentScript({ cmd: "end", value: "ok" }, function (response) {
		document.getElementById("status").innerText = "暂停";
	});
};

function sendMessageToContentScript(message, callback) {
	chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
		chrome.tabs.sendMessage(tabs[0].id, message, function (response) {
			if (callback) callback(response);
		});
	});
}

function copy(str, fn) {
	if (!str) {
		return;
	}
	var textArea = document.createElement("textarea");
	textArea.style.background = "transparent";
	textArea.value = str;
	document.body.appendChild(textArea);
	textArea.select();
	try {
		document.execCommand("copy");
		fn();
	} catch (err) {
		console.log(err);
	}
	document.body.removeChild(textArea);
}
